package com.higold.cloud.view;

import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.view.View;

import com.db.chart.Tools;
import com.db.chart.animation.Animation;
import com.db.chart.model.BarSet;
import com.db.chart.renderer.XRenderer;
import com.db.chart.renderer.YRenderer;
import com.db.chart.tooltip.Tooltip;
import com.db.chart.view.BarChartView;
import com.higold.cloud.R;


public class RankChart {

    protected boolean firstStage;

    private final Context mContext;

    private final BarChartView mChart;

    private final String[] mLabels = {"A", "B", "C", "D"};

    private final float[][] mValues = {{6.5f, 8.5f, 2.5f, 10f}, {7.5f, 3.5f, 5.5f, 4f}};


    public RankChart(BarChartView barChartView, Context context) {

        super();

        mChart = barChartView;
        mContext = context;
    }

    public void init() {

    }

    public void show() {
        firstStage = false;
        // Data
        BarSet barSet = new BarSet(mLabels, mValues[1]);
        barSet.setColor(Color.parseColor("#48A9F8"));
        mChart.addData(barSet);

        mChart.setBarSpacing(Tools.fromDpToPx(90));
        mChart.setRoundCorners(Tools.fromDpToPx(30));
        mChart.setBarBackgroundColor(Color.WHITE);

        // Chart
        mChart.setXAxis(false)
                .setYAxis(false)
                .setXLabels(XRenderer.LabelPosition.OUTSIDE)
                .setYLabels(YRenderer.LabelPosition.NONE)
                .setLabelsColor(Color.parseColor("#86705c"))
                .setAxisColor(Color.parseColor("#86705c"));

        int[] order = {1, 0, 2, 3};
        Runnable chartOneAction = this::showTooltip;
        mChart.show(new Animation().setOverlap(.7f, order).setEndAction(chartOneAction));
    }

    public void update() {
        firstStage = !firstStage;

        mChart.dismissAllTooltips();
        if (firstStage) mChart.updateValues(0, mValues[1]);
        else mChart.updateValues(0, mValues[0]);
        mChart.notifyDataUpdate();
    }

    public void dismiss(Runnable action) {

        mChart.dismissAllTooltips();
        int[] order = {0, 2, 1, 3};
        mChart.dismiss(new Animation().setOverlap(.7f, order).setEndAction(action));
    }


    private void showTooltip() {

        // Tooltip
        Tooltip tip = new Tooltip(mContext, R.layout.square_tooltip);

        tip.setEnterAnimation(PropertyValuesHolder.ofFloat(View.ALPHA, 1),
                PropertyValuesHolder.ofFloat(View.SCALE_X, 1f),
                PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f)).setDuration(200);

        tip.setExitAnimation(PropertyValuesHolder.ofFloat(View.ALPHA, 0),
                PropertyValuesHolder.ofFloat(View.SCALE_X, 0f),
                PropertyValuesHolder.ofFloat(View.SCALE_Y, 0f)).setDuration(200);
        tip.setVerticalAlignment(Tooltip.Alignment.BOTTOM_TOP);
        tip.setDimensions((int) Tools.fromDpToPx(25), (int) Tools.fromDpToPx(25));
        tip.setMargins(0, 0, 0, (int) Tools.fromDpToPx(10));
        tip.prepare(mChart.getEntriesArea(0).get(2), 0);

        mChart.showTooltip(tip, true);
    }

}
